const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show_tab_bar: !0,
    gangweiList:['社区工作者','网格员','工会','社区公益岗','全部岗位'],
    page: 49
  },

  closeselect() {
    var that = this;
    that.setData({
      bindpro: !1,
      bindgangwei: !1,
    })
  },
  //点击选择-岗位类别gangwei
  choisegangwei() {
    var that = this;
    that.setData({
      bindgangwei: 1,
    })
  },
  //点击选择-岗位类别gangwei
  setgangwei(e) {
    var that = this;
    console.log(e.currentTarget.dataset.gangwei)
    that.setData({
      gangwei: e.currentTarget.dataset.gangwei,
      bindgangwei: !1
    })
  },
  //点击选择-省份地市
  choiseProvince() {
    var that = this;
    that.setData({
      bindpro: 1,
    })
  },
  //一级联动-省份
  province() {
    var that = this;
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=43761&callback=?', //接收url
      data: {
        level: '1',
        province: ''
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].province)
        }
        // console.log(list)
        that.setData({
          provinceList: list
        });
      }
    })
  },
  //点击选择-省份
  setProvince(e) {
    var that = this;
    console.log(e.currentTarget.dataset.province)
    that.setData({
      province: e.currentTarget.dataset.province
    })
    that.city()
  },
  //点击重新选择-省份
  rechoiseprovince() {
    var that = this;
    that.setData({
      cityList: !1,
      city: !1
    })
  },
  //二级联动-地市
  city() {
    var that = this;
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=43761&callback=?', //接收url
      data: {
        level: '2',
        grfiled: 'province',
        grtext: that.data.province
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        // console.log(theJson)
        var list = [];
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].city)
        }
        console.log(list)
        that.setData({
          cityList: list
        });
      }
    })
  },
  //点击选择-地市
  setCity(e) {
    var that = this;
    console.log(e.currentTarget.dataset.city)
    if (e.currentTarget.dataset.city == '不限') {
      var city = that.data.province
    } else {
      var city = e.currentTarget.dataset.city
    }
    console.log(city)
    that.setData({
      city: city,
      cityList: !1,
      bindpro: !1
    })
  },
 


  getPhoneNumber(e) {
    var that = this;
    var flag = that.data.flag;
    var area = that.data.area;
    var agent = that.data.geneal;

    if (that.data.city && that.data.gangwei ) {
      if(flag){
      wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      that.setData({
        mobile: res.result,
      })
      console.log(res.result)
      
      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
        method: 'POST',
        data: {
          activeId: 43,
          mobile: res.result,
          areaCode: that.data.area,
          agent: that.data.geneal,
          pageId: that.data.page
        },
        success: res => {
          console.log(res);
        }
      })

      wx.setStorageSync('phone', that.data.mobile)
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/biaodan/register?actid=48544&callback=?', //接收url
        data: {
          phone: res.result,
          fenxiao: that.data.area,
          geneal: that.data.geneal,
          city:that.data.city,
          province:that.data.province,
          gangwei:that.data.gangwei,
          isagree: true
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
           console.log(theJson)
          
              
            var phone = wx.getStorageSync('phone')
            wx.showModal({
              title: '提示',
              content: '是否需要推送公告发布时间提醒',
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx.requestSubscribeMessage({
                    tmplIds: ['Iy261cAlHD10zOmP1CmQCxzEh55dpsAsVrrY7GqafHs'], 
                    success(res) {
                      db.collection('userInfo').where({
                        _openid: app.globalData.openid,
                      }).get({
                        success: res => {
                          db.collection('userInfo').add({
                            data: {
                              phone: phone,
                              city: that.data.city,
                              province:that.data.province,
                              gangwei: that.data.gangwei
                            },
                            //添加成功的回调函数
                            success(res) {
                              console.log(res)
                              wx.showToast({
                                title: '预约成功！',
                              })
                              console.log('cloud get success!')
                            }
                          });
                        },
                      })
                    },
                    fail(res) {
                      console.log(res);
                    }
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
        }
      })

      

    }).catch(err => {
      console.error(err);
    })} else {
      wx.showToast({
        title: '请先勾选隐私协议！',
        icon: 'none'
      })
    }} else {
      wx.showToast({
        title: '请完整填写信息',
        duration: 2000,
        icon: 'none'
      })
    }


  },
  xz: function (e) {
    var _ = this;
    var flag = e.detail.value[0];
    if (flag == undefined) {
      flag = false
    } else {
      flag = true
    }
    _.setData({
      flag
    })
    console.log(_.data.flag)
  },
  xy: function (e) {
    var _ = this;
    wx.navigateTo({
      url: '../agreement/agreement',
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.agent,
      page: options.page
    })
  },

  toxn: function () {
    wx.navigateTo({
      url: '../xiaoneng/xiaoneng',
    })
  },
  to2: function () {
    wx.navigateToMiniProgram({
      appId: 'wxca86930ec3e80717', //小程序appid
      path: 'page/course/details?fenxiang=1&id=245116', //跳转关联小程序app.json配置里面的地址
      //**重点**要打开的小程序版本，有效值 develop（开发版），trial（体验版），release（正式版） 
      envVersion: 'release',
      success(res) {
        console.log('打开成功！')
      }
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '山东社区招聘公告预约',
      path: 'pages/index/index'
    }
  },
  /**分享到朋友圈*/
  onShareTimeline: function () {
    return {
      title: '山东社区招聘公告预约',
      path: 'pages/index/index'
    }
  }
})